package com.stylefeng.guns.rest.modular.user;

import com.alibaba.dubbo.config.annotation.Reference;
import com.stylefeng.guns.api.user.service.UserAPI;
import com.stylefeng.guns.api.film.vo.UserInfoModel;
import com.stylefeng.guns.api.film.vo.UserModel;
import com.stylefeng.guns.rest.common.CurrentUser;
import com.stylefeng.guns.rest.modular.vo.ResponseVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @创建人 lIUJF
 * @创建时间 2019/1/7
 * @描述:
 */

@RequestMapping("/user/")
@RestController
public class UserController {

	@Reference(interfaceClass = UserAPI.class,check = false)
	private UserAPI userAPI;

	@RequestMapping(value = "register", method = RequestMethod.POST)
	public ResponseVO register(UserModel userModel){
		if (StringUtils.isBlank(userModel.getUserName()) || StringUtils.isBlank(userModel.getPassWord())) {
			return ResponseVO.serviceFail("注册信息填写不完整");
		}
		boolean isSuccess = userAPI.register(userModel);
		if (isSuccess) {
			return ResponseVO.success("注册成功");
		} else {
			return ResponseVO.serviceFail("注册失败");
		}
	}

	@RequestMapping(value = "checkUserName", method = RequestMethod.POST)
	public ResponseVO checkUserName(String userName){
		if (StringUtils.isBlank(userName)) {
			return ResponseVO.serviceFail("用户名不为空");
		}
		boolean isSuccess = userAPI.checkUserName(userName);
		if (isSuccess) {
			return ResponseVO.success("用户名可用");
		} else {
			return ResponseVO.serviceFail("用户名不可用");
		}
	}

	/**
	 * @return 应用 ：
	 * 1.前端存储jwt【七天】:  jwt的刷新
	 * 2.服务器会存储活动用户信息【30分钟】
	 * 3.JWT里的userId为key,查找活跃用户
	 * <p>
	 * 退出 ：
	 * 1.前端删除jwt
	 * 2.后端服务器删除活跃用户缓存
	 * 现状 :
	 * 1.前端删除jwt
	 */
	@RequestMapping(value = "logut", method = RequestMethod.GET)
	public ResponseVO logut(){
		String userId = CurrentUser.getCurrentUser();
		if (StringUtils.isBlank(userId)) {
			return ResponseVO.serviceFail("用户名不为空");
		} else {
			return ResponseVO.success("");
		}
	}

	@RequestMapping(value = "getUserInfo", method = RequestMethod.GET)
	public ResponseVO getUserInfo(){
		String userId = CurrentUser.getCurrentUser();
		if (StringUtils.isBlank(userId)) {
			return ResponseVO.appFail("当前用户未登陆");
		} else {
			int uuid = Integer.parseInt(userId);
			UserInfoModel userInfo = userAPI.getUserInfo(uuid);
			if (null != userInfo) {
				return ResponseVO.success(userInfo);
			} else {
				return ResponseVO.appFail("用户信息查询失败");
			}
		}
	}

	@RequestMapping(value = "updateUserInfo", method = RequestMethod.POST)
	public ResponseVO updateUserInfo(UserInfoModel userInfoModel){
		String userId = CurrentUser.getCurrentUser();
		if (StringUtils.isBlank(userId)) {
			return ResponseVO.appFail("当前用户未登陆");
		} else {
			int uuid = Integer.parseInt(userId);
			if (uuid == userInfoModel.getUuid()) {
				UserInfoModel userInfo = userAPI.updateUserInfo(userInfoModel);
				if (null != userInfo) {
					return ResponseVO.success(userInfo);
				} else {
					return ResponseVO.appFail("用户信息修改失败");
				}
			} else {
				return ResponseVO.serviceFail("请修改您个人的信息");
			}
		}
	}
}
